<template>
  <div class="excel-container">
    <div class="app-contsinrt">
      <el-caed>
        <!-- 使用组件 -->
        <upload-excel
          :before-upload="handleBeforeUpload"
          :on-success="handleUploadSuccess"
        />
      </el-caed>
    </div>
  </div>
</template>

<script>
import { addEmployeeBatchAPI } from "@/api/employees";
export default {
  name: "Excel",
  methods: {
    //上传前的校验
    handleBeforeUpload(file) {
      //   file: 选择 / 拖拽的excel文件
      //   可以教研文件大小  通过返回true 不通过选择false
      //   只有true能出发
      const isMt5M = file.size / 1024 / 1024 > 5;
      if (isMt5M) {
        this.$message.warning("太大了，进不来");
        //不通过
        return false;
      }
      return true;
    },
    handleUploadSuccess({ haeder, results }) {
      // header（excel）表头字符串的数组
      // excle的表体对象数组
      const zh2EnKey = {
        入职日期: "timeOfEntry",
        姓名: "username",
        工号: "workNumber",
        手机号: "mobile",
        转正日期: "correctionTime",
      };
      const employessList = results.map((item) => {
        const obj = {};
        Object.keys(item).forEach((key) => {
          // 根据中文key得到英文key
          const enKey = zh2EnKey[key];
          //   给obj添加英文key并赋值
          if (enKey === "timeOfEntry" || enKey === "correctionTime") {
            obj[enKey] = dayjs(getJsDateFromExcel(item[key])).format(
              "YYYY-MM-DD"
            );
          } else {
            obj[enKey] = item[key];
          }
        });
        // 返回
        return obj;
      });
      addEmployeeBatchAPI(employessList);
      this.$message.success("进来了");
      this.$router.go(-1);
    },
  },
};
</script>

<style>
</style>